#!/bin/bash

kernel_version="$1"

if [ "$kernel_version" = "" ]; then
  echo "Error: missing kernel version argument."
  exit 1
fi

if [ ! -e qla2xxx-orig/${kernel_version/^*} ]; then
  "$(dirname "$0")/extract-qla2xxx-orig" "$kernel_version" || exit $?
fi

kernel_version=${kernel_version/^*}

mkdir -p in-tree-patches/"${kernel_version}"

for g in Kconfig *.[ch]; do
  f1="qla2xxx-orig/${kernel_version}/$g"
  f2="$g"
  f3="in-tree-patches/${kernel_version}/$g.patch"
  if [ "$f1" -nt "$f3" -o "$f2" -nt "$f3" ]; then
    if [ -e "$f1" ]; then
      diff -up "$f1" "$f2" > "$f3"
    else
      diff -up /dev/null "$f2" > "$f3"
    fi
  fi
done

for g in include/trace/events/qla.h; do
  f1="qla2xxx-orig/${kernel_version}/$g"
  f2="$g"
  f3="in-tree-patches/${kernel_version}/${g#include/trace/events/}.patch"
  if [ "$f1" -nt "$f3" -o "$f2" -nt "$f3" ]; then
    if [ -e "$f1" ]; then
      diff -up "$f1" "$f2" > "$f3"
    else
      diff -up /dev/null "$f2" > "$f3"
    fi
  fi
done

for g in Makefile; do
  f1="qla2xxx-orig/${kernel_version}/$g"
  f2="${g}_in-tree"
  f3="in-tree-patches/${kernel_version}/$g.patch"
  if [ "$f1" -nt "$f3" -o "$f2" -nt "$f3" ]; then
    diff -up "$f1" "$f2" > "$f3"
  fi
done
